/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.ServiceQos;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class RemoteServiceConfigContent extends Content {

	// private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private BifangGWTServiceAsync service;

	private VerticalPanel panel = null;

	private ListBox log, acl, retry;

	private ArrayList<ServiceQos> qos;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			Window.alert(cons.modifyOK());

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RemoteServiceConfigContent(ServiceMessages cons,
			BifangGWTServiceAsync ser, GwtContext context,
			ArrayList<ServiceQos> sqos) {
		this.cons = cons;
		this.context = context;
		this.qos = sqos;
		this.service = ser;

		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		HTML h = new HTML(cons.configService());
		panel.add(h);

		Grid table = new Grid(3, 2);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");

		table.setHTML(0, 0, cons.log());
		table.setHTML(1, 0, cons.acl());
		table.setHTML(2, 0, cons.retry());

		log = new ListBox();
		log.addItem(cons.no(), EntityConstants.QOS_NO);
		log.addItem(cons.yes(), EntityConstants.QOS_YES);

		acl = new ListBox();
		acl.addItem(cons.no(), EntityConstants.QOS_NO);
		acl.addItem(cons.yes(), EntityConstants.QOS_YES);

		retry = new ListBox();
		retry.addItem(cons.no(), EntityConstants.QOS_NO);
		retry.addItem(cons.yes(), EntityConstants.QOS_YES);

		for (int i = 0, j = qos.size(); i < j; i++) {
			ServiceQos q = (ServiceQos) qos.get(i);
			if (q.getName().equals(EntityConstants.QOS_LOG)) {
				if (q.getValue().equals(EntityConstants.QOS_YES)) {
					log.setSelectedIndex(1);
				} else {
					log.setSelectedIndex(0);
				}
			} else if (q.getName().equals(EntityConstants.QOS_ACL)) {
				if (q.getValue().equals(EntityConstants.QOS_YES)) {
					acl.setSelectedIndex(1);
				} else {
					acl.setSelectedIndex(0);
				}
			} else if (q.getName().equals(EntityConstants.QOS_RETRY)) {
				if (q.getValue().equals(EntityConstants.QOS_YES)) {
					retry.setSelectedIndex(1);
				} else {
					retry.setSelectedIndex(0);
				}
			}
		}

		table.setWidget(0, 1, log);
		table.setWidget(1, 1, acl);
		table.setWidget(2, 1, retry);

		Button p = new Button(cons.modify());
		p.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				for (int i = 0, j = qos.size(); i < j; i++) {
					ServiceQos q = (ServiceQos) qos.get(i);
					if (q.getName().equals(EntityConstants.QOS_LOG)) {
						if (log.getSelectedIndex() == 0) {
							q.setValue(EntityConstants.QOS_NO);
						} else {
							q.setValue(EntityConstants.QOS_YES);
						}
					} else if (q.getName().equals(EntityConstants.QOS_ACL)) {
						if (acl.getSelectedIndex() == 0) {
							q.setValue(EntityConstants.QOS_NO);
						} else {
							q.setValue(EntityConstants.QOS_YES);
						}
					} else if (q.getName().equals(EntityConstants.QOS_RETRY)) {
						if (retry.getSelectedIndex() == 0) {
							q.setValue(EntityConstants.QOS_NO);
						} else {
							q.setValue(EntityConstants.QOS_YES);
						}
					}
				}
				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.ModifyServiceQosAction");
				req.setRequest(qos);
				service.call(req, cb);

			}
		});

		panel.add(table);
		if (PrivilegChecker.hasPrivilege(context, null))
			panel.add(p);

		// panel.add(b);

	}

}
